//package com.software.graduationproject.modules.sys.web;
//
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.core.util.StringUtil;
//
//import com.software.graduationproject.commons.config.ApplicationConstants;
//import com.software.graduationproject.commons.enums.CommonFlagType;
//import com.software.graduationproject.commons.enums.LogType;
//import com.software.graduationproject.commons.log.Log;
//import com.software.graduationproject.commons.util.Result;
//import com.software.graduationproject.commons.util.Results;
//import com.software.graduationproject.commons.util.VerifyCodeUtils;
//import com.software.graduationproject.modules.sys.entity.SysUser;
//import com.software.graduationproject.modules.sys.object.SysUserVo;
//import com.software.graduationproject.modules.sys.service.SysUserService;
//import com.software.graduationproject.modules.sys.object.SysUserCreateQo;
//
//import com.software.graduationproject.commons.redis.RedisCache;
//import com.v2coding.validate.CreateGroup;
//
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import org.hibernate.service.spi.ServiceException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.*;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.util.Base64;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
///**
// * SysUserController : 系统用户表
// * ---------------------------------------------------------------------------------------------------------------------
// */
//@Validated
//@RestController
//@RequestMapping("/Sys/SysUser")
//@Api(value = "API - 系统用户表", tags = {"系统用户表接口"})
//public class SysUserController {
//
//    @Autowired
//    private SysUserService sysUserService;
//
//    @Autowired
//    private RedisCache redisCache;
//
//    @Autowired
//    private PasswordEncoder passwordEncoder;
//
//    @PostMapping("/create")
//    @ApiOperation("新增系统用户表")
//    public Result createQo(@RequestBody @Validated(CreateGroup.class) SysUserCreateQo qo) {
//        sysUserService.createQo(qo);
//        return Results.success();
//    }
////
////    @PostMapping("/update")
////    @ApiOperation("修改系统用户表")
////    public Result updateQo(@RequestBody @Validated(UpdateGroup.class) SysUserUpdateQo qo) {
////        sysUserService.updateQo(qo);
////        return Results.success();
////    }
////
////    @PostMapping("/delete")
////    @ApiOperation("删除系统用户表")
////    public Result delete(@RequestBody Long id) {
////        sysUserService.delete(id);
////        return Results.success();
////    }
////
////    @PostMapping("/deleteList")
////    @ApiOperation("批量删除系统用户表")
////    public Result deleteList(@RequestBody Long []ids) {
////        sysUserService.deleteList(ids);
////        return Results.success();
////    }
////
////    @GetMapping("/select")
////    @ApiOperation("查询系统用户表详情")
////    public Result<SysUserVo> selectVo(Long id) {
////        SysUserVo sysUserVo = sysUserService.selectVo(id);
////        return Results.success(sysUserVo);
////    }
////
//    @GetMapping("/selectList")
//    @ApiOperation("查询系统用户表列表")
//    public Result<List<SysUserVo>> selectListVo() {
//        List<SysUserVo> sysUserVoList = sysUserService.selectListVo();
//        return Results.success(sysUserVoList);
//    }
////
////    @GetMapping("/selectPage")
////    @ApiOperation("查询系统用户表分页")
////    @SelectParam({"sort.id=desc"})
////    @ApiImplicitParams({ //
////            @ApiImplicitParam(name = "select", value = "查询条件", required = false, dataTypeClass = String.class, paramType = "query"),//
////    })
////    public Result<Page<SysUserVo>> selectPageVo(@ApiIgnore Select select) {
////        Page<SysUserVo> sysUserVoPage = sysUserService.selectPage(select, SysUserVo.class);
////        return Results.success(sysUserVoPage);
////    }
////
////    @GetMapping("/export")
////    @ApiOperation("导出系统用户表")
////    @SelectParam({"sort.id=desc"})
////    @ApiImplicitParams({ //
////            @ApiImplicitParam(name = "select", value = "查询条件", required = false, dataTypeClass = String.class, paramType = "query"),//
////            @ApiImplicitParam(name = "columns", value = "导出列", required = true, dataTypeClass = String.class, paramType = "query"),//
////    })
////    public void export(@ApiIgnore Select select,@RequestParam(defaultValue = "导出.xls") String title, String columns, String headers) {
////        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
////        HttpServletResponse response = requestAttributes.getResponse();
////        // @formatter:off
////        Excels.ExportParams params = new Excels.ExportParams()
////                .setList(sysUserService.selectList(select, SysUserVo.class))
////                .setClazz(SysUser.class)
////                .setColumns(columns)
////                .setHeaders(headers);
////        // @formatter:on
////        Excels.export(params, response);
////    }
//
//    @PostMapping("/login")
//    @Log(logType = LogType.LOGIN, ignoreParams = true)
//    @ApiOperation("登录")
//    public Result login(@RequestBody SysUser user){
//        // 校验用户参数
//        if (StringUtil.isBlank(user.getUsername()) || StringUtil.isBlank(user.getPassword())) {
//            return Results.failure("帐号密码错误");
//        }
//        SysUser sysUser = sysUserService.selectByUserName(user.getUsername());
//        if (sysUser == null) {
//            return Results.failure("帐号密码错误");
//        }
//        Integer errorcount = redisCache.getCacheObject(ApplicationConstants.CACHE_LOGIN_ERROR + ":" + sysUser.getId());
//        errorcount = (errorcount == null ? 0 : errorcount);
//        if (ObjectUtil.equal(sysUser.getLockState(), CommonFlagType.Y.getValue()) || errorcount >= ApplicationConstants.LOGIN_ERROR_COUNT_MAX) {
//            return Results.failure("账号已被锁定");
//        }
//        // 密码错误时处理
//        if (!passwordEncoder.matches(user.getPassword(), sysUser.getPassword())) {
//            errorcount++;
//            redisCache.setCacheObject(ApplicationConstants.CACHE_LOGIN_ERROR + ":" + sysUser.getId(), errorcount, ApplicationConstants.LOGIN_ERROR_LOCK_TIME, TimeUnit.SECONDS);
//            // 最大错误次数以内提示
//            if (errorcount < ApplicationConstants.LOGIN_ERROR_COUNT_MAX) {
//                return Results.failure("登录错误" + errorcount + "次,再错误" + (ApplicationConstants.LOGIN_ERROR_COUNT_MAX - errorcount) + "次之后将锁定账号");
//            }
//            sysUserService.lock(sysUser);
//            // 超过错误次数提示
//            return Results.failure("账号已被锁定，" + ApplicationConstants.LOGIN_ERROR_LOCK_TIME / 60 + "分钟内无法登录");
//        }
//        if (sysUser.getLimitation().getTime() < System.currentTimeMillis()) {
//            return Results.failure("账号时效到期");
//        }
//        // TODO 角色校验
//        // TODO 组织校验
//
//        redisCache.deleteObject(ApplicationConstants.CACHE_LOGIN_ERROR + ":" + sysUser.getId());
//
//        return sysUserService.login(user);
//    }
//
//    @PostMapping("/logout")
//    @ApiOperation("退出账号")
//    public Result logout(){
//        // 退出登录
//        sysUserService.logout();
//        return Results.success();
//    }
//
//    @GetMapping("/generatorCode")
//    @ApiOperation("生成验证码")
//    public void generatorCode(HttpServletRequest request, HttpServletResponse response) {
//        try {
//            HttpSession session = request.getSession();
//            response.setContentType("application/json");
//            response.setHeader("Pragma", "No-cache");
//            response.setHeader("Cache-Control", "no-cache");
//            response.setDateHeader("Expire", 0);
//            String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
//            session.setAttribute("code", verifyCode);
//            // 将验证码图片转换为Base64编码
//            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//            VerifyCodeUtils.outputImage(200, 80, byteArrayOutputStream, verifyCode);
//            byte[] imageBytes = byteArrayOutputStream.toByteArray();
//            // 手动添加Base64编码头部信息
//            String base64Image = "data:image/png;base64," + Base64.getEncoder().encodeToString(imageBytes);
//            // 将Base64编码的图片和验证码一起包含在响应中
//            response.getWriter().write("{\"base64Image\": \"" + base64Image + "\", \"verifyCode\": \"" + verifyCode + "\"}");
//        } catch (IOException e) {
//            throw new ServiceException("生成验证码失败");
//        }
//    }
//}
